home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / doslynx / src / new.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  5.7 KB  |  265 lines

  1. //      This file extracted from the turbovision library in order to modify
  2. //      the new operator.
  3. //              03-03-94        Garrett Arch Blythe
  4.  
  5. /*------------------------------------------------------------*/
  6. /* filename -       new.cpp                                   */
  7. /*------------------------------------------------------------*/
  8.  
  9. /*------------------------------------------------------------*/
  10. /*                                                            */
  11. /*    Turbo Vision -  Version 1.0                             */
  12. /*                                                            */
  13. /*                                                            */
  14. /*    Copyright (c) 1991 by Borland International             */
  15. /*    All Rights Reserved.                                    */
  16. /*                                                            */
  17. /*------------------------------------------------------------*/
  18.  
  19. #include<new.h>
  20.  
  21. #if !defined( __MEM_H )
  22. #include <Mem.h>
  23. #endif  // __MEM_H
  24.  
  25. #if !defined( __ALLOC_H )
  26. #include <Alloc.h>
  27. #endif  // __ALLOC_H
  28.  
  29. #if !defined( __STDLIB_H )
  30. #include <StdLib.h>
  31. #endif  // __STDLIB_H
  32.  
  33. #define Uses_TVMemMgr
  34. #include <tv.h>
  35.  
  36. #include"tcapture.h"
  37. #include"trace.h"
  38. #include<dos.h>
  39.  
  40. TBufListEntry * near TBufListEntry::bufList = 0;
  41.  
  42. TBufListEntry::TBufListEntry( void*& o ) : owner( o )
  43. {
  44.     next = bufList;
  45.     prev = 0;
  46.     bufList = this;
  47.     if( next != 0 )
  48.     next->prev = this;
  49. }
  50.  
  51. TBufListEntry::~TBufListEntry()
  52. {
  53.     owner = 0;
  54.     if( prev == 0 )
  55.     bufList = next;
  56.     else
  57.     prev->next = next;
  58.     if( next != 0 )
  59.     next->prev = prev;
  60. }
  61.  
  62. void *TBufListEntry::operator new( size_t sz, size_t extra )
  63. {
  64.     return malloc( sz + extra*sizeof( unsigned ) );
  65. }
  66.  
  67. void *TBufListEntry::operator new( size_t )
  68. {
  69.     return 0;
  70. }
  71.  
  72. void TBufListEntry::operator delete( void *b )
  73. {
  74.     free( b );
  75. }
  76.  
  77. Boolean TBufListEntry::freeHead()
  78. {
  79.     if( bufList == 0 )
  80.     return False;
  81.     else
  82.     {
  83.     delete bufList;
  84.     return True;
  85.     }
  86. }
  87.  
  88. void * near TVMemMgr::safetyPool = 0;
  89. size_t near TVMemMgr::safetyPoolSize = 0;
  90. int near TVMemMgr::inited = 0;
  91.  
  92. TVMemMgr memMgr;
  93.  
  94. TVMemMgr::TVMemMgr()
  95. {
  96.     if( !inited )
  97.     resizeSafetyPool();
  98. };
  99.  
  100. void TVMemMgr::resizeSafetyPool( size_t sz )
  101. {
  102.     inited = 1;
  103.     free( safetyPool );
  104.     if( sz == 0 )
  105.     safetyPool = 0;
  106.     else
  107.     safetyPool = malloc( sz );
  108.     safetyPoolSize = sz;
  109. }
  110.  
  111. int TVMemMgr::safetyPoolExhausted()
  112. {
  113.     return inited && (safetyPool == 0);
  114. }
  115.  
  116. void TVMemMgr::allocateDiscardable( void *&adr, size_t sz )
  117. {
  118.     if( safetyPoolExhausted() )
  119.     adr = 0;
  120.     else
  121.     {
  122.     TBufListEntry *newEntry = new( sz ) TBufListEntry( adr );
  123.     if( newEntry == 0 )
  124.         adr = 0;
  125.     else
  126.         adr = (char *)newEntry + sizeof(TBufListEntry);
  127.     }
  128. }
  129.  
  130. void TVMemMgr::freeDiscardable( void *block )
  131. {
  132.     delete (TBufListEntry *)((char *)block - sizeof(TBufListEntry));
  133. }
  134.  
  135.  
  136.  
  137. #if !defined( NDEBUG )
  138. const BLK_SIZE = 16;
  139. const BLK_DATA = 0xA6;
  140. #else
  141. const BLK_SIZE = 0;
  142. #endif
  143.  
  144. void *operator new(size_t sz)   {
  145. #ifndef RELEASE
  146.     if(heapcheck() < 0)     {
  147.         doslynxmessage("Heap corrupt.");
  148.         sleep(5);
  149.         exit(-1);
  150.     }
  151. #endif /* RELEASE */
  152.  
  153.     sz += BLK_SIZE;
  154.     if(sz == 0)     {
  155.         sz = 1;
  156.     }
  157.  
  158.     //      Attempt to allocate the requested memory.
  159.     void *temp = malloc(sz);
  160.  
  161.     //      Free off draw buffers first in further attempts.
  162.     while(temp == 0 && TBufListEntry::freeHead() == True)   {
  163.         temp = malloc( sz );
  164.     }
  165.  
  166.     //      Still unsuccessful....
  167.     if(temp == 0)   {
  168.         //      If we've already gotten rid of the pool....
  169.         if(TVMemMgr::safetyPoolExhausted())     {
  170.             while(temp == 0)        {
  171.                 //      Free loaded HTexts third.
  172.                 _new_handler();
  173.                 temp = malloc(sz);
  174.             }
  175.         }
  176.         else    {
  177.             //      Free off safety pool second.
  178.             TVMemMgr::resizeSafetyPool(0);
  179.             temp = malloc(sz);
  180.             if(temp == 0)   {
  181.                 while(temp == 0)        {
  182.                     //      Free loaded HTexts third and
  183.                     //      the really_safe_pool.
  184.                     _new_handler();
  185.                     temp = malloc(sz);
  186.                 }
  187.             }
  188.         }
  189.     }
  190.  
  191. #if !defined( NDEBUG )
  192.     memset(temp, BLK_DATA, BLK_SIZE);
  193. #endif
  194.  
  195.     return((char *)temp + BLK_SIZE);
  196. }
  197.  
  198.  
  199. #if !defined( NDEBUG )
  200. static void check( void *blk )
  201. {
  202.     for( int i = 0; i < BLK_SIZE; i++ ) {
  203.     if(*((unsigned char *)blk + 1) != BLK_DATA)     {
  204.         exit(-1);
  205.     }
  206.     }
  207. }
  208. #endif
  209.  
  210. void operator delete(void *blk) {
  211.  
  212. #ifndef RELEASE
  213.     if(heapcheck() < 0)     {
  214.         doslynxmessage("Heap corrupt.");
  215.         sleep(5);
  216.         exit(-1);
  217.     }
  218. #endif /* RELEASE */
  219.  
  220.     if(blk == 0)    {
  221.         return;
  222.     }
  223.     void *tmp = (char *)blk - BLK_SIZE;
  224.  
  225. #if !defined( NDEBUG )
  226.     check( tmp );
  227. #endif
  228.  
  229.     free(tmp);
  230.  
  231.     //      Put before resizeing the really safe pool since TurboVision
  232.     //      bases it's execution on the existance of the pool, if it
  233.     //      doesn't exist, then we crash.
  234.     if(TVMemMgr::safetyPoolExhausted())     {
  235.         //      Attempt to reinstate the safety pool if used up.
  236.         TVMemMgr::resizeSafetyPool();
  237.         //      If didn't get it, don't let the really safe pool
  238.         //      be built up either!
  239.         if(TVMemMgr::safetyPoolExhausted())     {
  240.             return;
  241.         }
  242.     }
  243.  
  244.     //      See if we need to try to fill back up our really safe pool.
  245.     //      Refill backwards.
  246.     if(really_safe_pool[Stop_Anchors] == NULL)      {
  247.         for(register signed short int ssi_counter = RSP_NUM - 1;
  248.             ssi_counter >= Stop_Anchors; ssi_counter--)     {
  249.  
  250.             if(really_safe_pool[ssi_counter] != NULL)       {
  251.                 continue;
  252.             }
  253.  
  254.             really_safe_pool[ssi_counter] = (char *)malloc(
  255.                 RSP_SIZE);
  256.  
  257.             //      Break loop if unable.  No use
  258.             //      in continuing.
  259.             if(really_safe_pool[ssi_counter] == NULL)       {
  260.                 break;
  261.             }
  262.         }
  263.     }
  264. }
  265.